package com.wangxinenpu.springbootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxinenpu.springbootdemo.dao.mapper.SystemMapper;
import com.wangxinenpu.springbootdemo.dataobject.dto.system.SystemDto;
import com.wangxinenpu.springbootdemo.dataobject.model.SysOrg;
import com.wangxinenpu.springbootdemo.dataobject.model.System;
import com.wangxinenpu.springbootdemo.dataobject.vo.system.SystemVo;
import com.wangxinenpu.springbootdemo.service.SysOrgService;
import com.wangxinenpu.springbootdemo.service.SystemService;
import com.wangxinenpu.springbootdemo.util.Generator;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统表 服务实现类
 * </p>
 *
 * @author =
 * @since 2024-06-14
 */
@Service
@Slf4j
@Setter ( onMethod_ = @Resource )
public class SystemServiceImpl extends ServiceImpl < SystemMapper,System > implements SystemService {

	private SystemMapper systemMapper;

	private SysOrgService sysOrgService;

	@Override
	public R < Page < SystemVo > > pageList( SystemDto dto ) {
		Page < SystemVo > page = new Page <>( dto.getPageNum() , dto.getPageSize() );
		Page < SystemVo > pageList = systemMapper.pageList( page , dto );
		pageList.getRecords().forEach( item -> {
			item.setOrgIds( List.of( item.getOrgId().split( "," ) ) );
		} );
		return R.ok( pageList );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public R < Boolean > delete( List < String > ids ) {
		if ( CollectionUtils.isEmpty( ids ) ) return R.failed( "所选删除内容为空" );
		return R.ok( removeByIds( ids ) );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public R < Boolean > savaOrUpdate( SystemVo dto ) {
		// 校验
		List < System > systemCodeList = list( new LambdaQueryWrapper < System >().eq( System::getSystemCode ,
				dto.getSystemCode() ).ne( StringUtils.isNotBlank( dto.getId() ) , System::getId , dto.getId() ) );
		if ( systemCodeList.size() > 0 ) {
			return R.failed( "系统编码已存在" );
		}

		List < System > systemNameList = list( new LambdaQueryWrapper < System >().eq( System::getSystemCode ,
				dto.getSystemName() ).ne( StringUtils.isNotBlank( dto.getId() ) , System::getId , dto.getId() ) );
		if ( systemNameList.size() > 0 ) {
			return R.failed( "系统名称已存在" );
		}

		// 保存

		// 组织关系保存
		sysOrgService.remove( new LambdaQueryWrapper < SysOrg >().eq( SysOrg::getSysId , dto.getId() ) );
		dto.getOrgIds().forEach( item -> {
			SysOrg sysOrg = new SysOrg();
			sysOrg.setSysId( dto.getId() );
			sysOrg.setOrgId( item );
			sysOrgService.save( sysOrg );
		} );

		System system = new System();
		BeanUtils.copyProperties( dto , system );
		if ( StringUtils.isNotBlank( dto.getId() ) ) {
			return R.ok( updateById( system ) );
		}
		else {
			system.setSystemKey( Generator.generateKey() );
			system.setSystemSecret( Generator.generateKey() );
			return R.ok( save( system ) );
		}
	}

	@Override
	public R < SystemVo > detail( String id ) {
		System system = getById( id );
		SystemVo systemVo = new SystemVo();
		BeanUtils.copyProperties( system , systemVo );
		sysOrgService.list( new LambdaQueryWrapper < SysOrg >().eq( SysOrg::getSysId , id ) ).forEach( item -> {
			systemVo.getOrgIds().add( item.getOrgId() );
		} );
		return R.ok( systemVo );
	}
}
